package com.tiankun.eurekasellinfo.service.impl;

import com.tiankun.eurekasellinfo.exception.BusinessException;
import com.tiankun.eurekasellinfo.mapper.MerchantMapper;
import com.tiankun.eurekasellinfo.pojo.Merchant;
import com.tiankun.eurekasellinfo.service.MerchantService;
import com.tiankun.eurekasellinfo.util.JsonResult;
import com.tiankun.eurekasellinfo.util.MD5Util;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @Author 杨天可
 * @Date 2020/11/13 10:39
 * @Version 1.0
 */
@Service("merchantService")
public class MerchantServiceImpl implements MerchantService {

    @Resource
    RedisTemplate<String, Object> redisTemplate;

    @Resource
    MerchantMapper merchantMapper;


    @Override
    public Map regist(Merchant merchant) throws ParseException {

        //该手机号是否已注册用户
        JsonResult jsonResult = new JsonResult();

        int i = merchantMapper.selectByPhone(merchant.getMerPhone());

        //如果已注册，提示用户已注册、更换手机号或者直接登陆
        if(i != 0){
            throw new BusinessException("该手机号已注册，请更换手机号或者直接登陆");
        }
        //判断验证码是否合法
        //从redis中获取验证码
        BoundValueOperations<String, Object> op = redisTemplate.boundValueOps(merchant.getMerPhone());
        Object o = op.get();
//        ValueOperations<String, Object> ob = redisTemplate.opsForValue();
//        Object o = ob.get(merchant.getMerPhone());

        System.out.println("商家电话："+merchant.getMerPhone());
        System.out.println("商家输入的验证码："+merchant.getYzm());
        System.out.println("获取的验证码:"+o);

        if (merchant.getYzm().equals(o)) {
            //如果合法，将个人信息+注册时间添加到数据库

            //创建盐
            String salt = MD5Util.createSalt();
            //加密密码、并存储加密后的密码和盐
            String encode = MD5Util.encode(merchant.getMerPassword() + salt);
            merchant.setMerSalt(salt);
            merchant.setMerPassword(encode);
            merchant.setMerCreated(new Date());

            //从身份证号获取生日并注入到 mer_birthday 字段
            String substring = merchant.getMerCard().substring(6, 14);
            String year=substring.substring(0,4);
            String mon=substring.substring(4,6);
            String sen=substring.substring(6);
            String birthday=year+"-"+mon+"-"+sen;
            SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd");
            Date parse = sdf.parse(birthday);
            merchant.setMerBirthday(parse);
            System.out.println("拼接的生日："+parse);
            System.out.println("设置进去的生日："+merchant.getMerBirthday());

            merchantMapper.regist(merchant);

            jsonResult.setCode("200");
            jsonResult.setMsg("注册成功");
        } else {
            //如果不合法，返回注册失败，验证码错误
            jsonResult.setCode("400");
            jsonResult.setMsg("验证码不合法");
        }
        return jsonResult.getValues();
    }

    @Override
    public Map login(String phone, String password) {
        JsonResult jsonResult = new JsonResult();

        //判断商家输入的号码是否注册
        int i = merchantMapper.selectByPhone(phone);
        if (i == 0){
            throw new BusinessException("您还未注册，请先注册");
        }

        //校验密码
        String salt = merchantMapper.findSalt(phone);
        String pwd = merchantMapper.findPwd(phone);

        if (pwd.equals(MD5Util.encode(password+salt))){
            Merchant mer = merchantMapper.findMer(phone);

            System.out.println("返回的商家信息"+mer);
            jsonResult.setCode("200");
            jsonResult.setMsg("登录成功");
            jsonResult.setData(mer);
        }else {
            jsonResult.setCode("400");
            jsonResult.setMsg("手机号或密码错误");
        }
        return jsonResult.getValues();
    }

    @Override
    public Map changepwd(String password, String phone, String yzm) {
        JsonResult jsonResult = new JsonResult();

        //判断验证码是否合法
        //从redis中获取验证码
        BoundValueOperations<String, Object> op = redisTemplate.boundValueOps(phone);
        Object o = op.get();
//        ValueOperations<String, Object> ob = redisTemplate.opsForValue();
//        Object o = ob.get(phone);

        System.out.println("获取的验证码:"+o);

        if (yzm.equals(o)) {
            //如果合法，将个人信息+注册时间添加到数据库

            //创建盐
            String salt = MD5Util.createSalt();
            //加密密码、并存储加密后的密码和盐
            String encode = MD5Util.encode(password + salt);
            System.out.println("加密后的密码"+encode);
            merchantMapper.changepwd(encode,salt,phone);

            jsonResult.setCode("200");
            jsonResult.setMsg("修改成功");
        } else {
            //如果不合法，返回注册失败，验证码错误
            jsonResult.setCode("400");
            jsonResult.setMsg("验证码不合法");
        }


        return jsonResult.getValues();
    }
}
